package handler

import (
	"article-mgr/cache"
	"article-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RopeOrderDetail(c *gin.Context) {
	id := c.Query("id")
	redis_key := "rope_order_detail_" + id
	var ropeOrderList []model.RepoOrderList
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.
			Table("repo_orders").
			Select(
				"repo_orders.id OrderId",
				"repos.pore_no PoreNo",
				"repos.pore_name PoreName",
				"repos.pore_address PoreAddress",
				"repo_orders.order_no OrderNo",
				"repo_orders.order_name OrderName",
			).
			Joins("LEFT JOIN repos ON repo_orders.pid = repos.id").
			Where("repo_orders.id=?", id).Find(&ropeOrderList)
		jsonData, _ := json.Marshal(ropeOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &ropeOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": ropeOrderList,
	})
	return
}

/*
SELECT

	repo_orders.id OrderId,
	repos.pore_no PoreNo,
	repos.pore_name PoreName,
	repos.pore_address PoreAddress,
	repo_orders.order_no OrderNo,
	repo_orders.order_name OrderName

FROM

	repo_orders
	 LEFT JOIN repos ON repo_orders.pid = repos.id
*/
func RopeOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	redis_key := "rope_order_list_" + strconv.Itoa(page)
	var ropeOrderList []model.RepoOrderList
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.
			Table("repo_orders").
			Select(
				"repo_orders.id OrderId",
				"repos.pore_no PoreNo",
				"repos.pore_name PoreName",
				"repos.pore_address PoreAddress",
				"repo_orders.order_no OrderNo",
				"repo_orders.order_name OrderName",
			).
			Joins("LEFT JOIN repos ON repo_orders.pid = repos.id").
			Offset(offset).Limit(size).Find(&ropeOrderList)
		jsonData, _ := json.Marshal(ropeOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &ropeOrderList)
	}
	var total int64
	model.DB.Model(&model.RepoOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  ropeOrderList,
		"total": total,
	})
	return
}

func RopeOrderAdd(c *gin.Context) {
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	order_no := c.PostForm("order_no")
	order_name := c.PostForm("order_name")
	data := model.RepoOrder{
		Pid:       pid,
		OrderNo:   order_no,
		OrderName: order_name,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func RopeAdd(c *gin.Context) {
	pore_no := c.PostForm("pore_no")
	pore_name := c.PostForm("pore_name")
	pore_address := c.PostForm("pore_address")
	if pore_no == "" || pore_name == "" || pore_address == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := model.Repo{
		PoreNo:      pore_no,
		PoreName:    pore_name,
		PoreAddress: pore_address,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func UserRoleAdd(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return
}

func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	/*
			SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.rid
		WHERE
		  user_roles.uid = 1
	*/
	var roles model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid=?", uid).Debug().
		Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": roles,
	})
	return
}

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
	*/
	var node model.Node
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.rid=?", rid).
		Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
